//
// PropertyDefinition.cs
//
// Author:
//   Jb Evain (jbevain@gmail.com)
//
// (C) 2005 Jb Evain
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Text;

namespace Mono.Cecil {

	public sealed class PropertyDefinition : PropertyReference,
		IMemberDefinition, ICustomAttributeProvider, IConstantProvider {

		bool? has_this;
		PropertyAttributes attributes;

		CustomAttributeCollection custom_attributes;

		MethodDefinition get_method;
		MethodDefinition set_method;

		object constant = Constants.NotResolved;

		public PropertyAttributes Attributes {
			get { return attributes; }
			set { attributes = value; }
		}

		public bool HasThis {
			get {
				if (has_this.HasValue)
					return has_this.Value;

				if (GetMethod != null)
					return get_method.HasThis;

				if (SetMethod != null)
					return set_method.HasThis;

				return false;
			}
			set { has_this = value; }
		}

		public bool HasCustomAttributes {
			get {
				if (custom_attributes != null)
					return custom_attributes.Count > 0;

				if (HasImage)
					return DeclaringType.Module.Read (this, (type, reader) => reader.HasCustomAttributes (type));

				return false;
			}
		}

		public CustomAttributeCollection CustomAttributes {
			get {
				if (custom_attributes != null)
					return custom_attributes;

				if (HasImage)
					return custom_attributes = DeclaringType.Module.Read (this, (type, reader) => reader.ReadCustomAttributes (type));

				return custom_attributes = new CustomAttributeCollection ();
			}
		}

		public MethodDefinition GetMethod {
			get {
				if (get_method != null)
					return get_method;

				InitializeMethods ();
				return get_method;
			}
			set { get_method = value; }
		}

		public MethodDefinition SetMethod {
			get {
				if (set_method != null)
					return set_method;

				InitializeMethods ();
				return set_method;
			}
			set { set_method = value; }
		}

		public override ParameterDefinitionCollection Parameters {
			get {
				if (get_method != null)
					return MirrorParameters (get_method, 0);

				if (set_method != null)
					return MirrorParameters (set_method, 1);

				return new ParameterDefinitionCollection ();
			}
		}

		ParameterDefinitionCollection MirrorParameters (MethodDefinition method, int start)
		{
			var parameters = new ParameterDefinitionCollection ();
			if (!method.HasParameters)
				return parameters;

			for (int i = start; i < method.Parameters.Count; i++)
				parameters.Insert (i, method.Parameters [i]);

			return parameters;
		}

		public bool HasConstant {
			get {
				ResolveConstant ();

				return constant != Constants.NoValue;
			}
			set { if (!value) constant = Constants.NoValue; }
		}

		public object Constant {
			get { return HasConstant ? constant : null;	}
			set { constant = value; }
		}

		void ResolveConstant ()
		{
			if (constant != Constants.NotResolved)
				return;

			if (!HasImage) {
				constant = Constants.NoValue;
				return;
			}

			constant = DeclaringType.Module.Read (this, (field, reader) => reader.ReadConstant (field));
		}

		#region PropertyAttributes

		public bool IsSpecialName {
			get { return (attributes & PropertyAttributes.SpecialName) != 0; }
			set {
				if (value)
					attributes |= PropertyAttributes.SpecialName;
				else
					attributes &= ~PropertyAttributes.SpecialName;
			}
		}

		public bool IsRuntimeSpecialName {
			get { return (attributes & PropertyAttributes.RTSpecialName) != 0; }
			set {
				if (value)
					attributes |= PropertyAttributes.RTSpecialName;
				else
					attributes &= ~PropertyAttributes.RTSpecialName;
			}
		}

		public bool HasDefault {
			get { return (attributes & PropertyAttributes.HasDefault) != 0; }
			set {
				if (value)
					attributes |= PropertyAttributes.HasDefault;
				else
					attributes &= ~PropertyAttributes.HasDefault;
			}
		}

		#endregion

		public new TypeDefinition DeclaringType {
			get { return (TypeDefinition) base.DeclaringType; }
			set { base.DeclaringType = value; }
		}

		public override string FullName {
			get {
				var builder = new StringBuilder ();
				builder.Append (PropertyType.ToString ());
				builder.Append (' ');
				builder.Append (MemberFullName ());
				builder.Append ('(');
				var parameters = Parameters;
				for (int i = 0; i < parameters.Count; i++) {
					if (i > 0)
						builder.Append (',');
					builder.Append (parameters [i].ParameterType.FullName);
				}
				builder.Append (')');
				return builder.ToString ();
			}
		}

		public PropertyDefinition (string name, TypeReference propertyType, PropertyAttributes attrs) : base (name, propertyType)
		{
			attributes = attrs;
		}

		void InitializeMethods ()
		{
			if (get_method != null || set_method != null)
				return;

			var type = DeclaringType;
			if (type == null)
				return;

			var module = type.Module;
			if (module == null || !module.HasImage)
				return;

			module.Read (this, (property, reader) => reader.ReadMethods (property));
		}

		/*public static MethodDefinition CreateGetMethod (PropertyDefinition prop)
		{
			MethodDefinition get = new MethodDefinition (
				string.Concat ("get_", prop.Name), (MethodAttributes) 0, prop.PropertyType);
			prop.GetMethod = get;
			return get;
		}

		public static MethodDefinition CreateSetMethod (PropertyDefinition prop)
		{
			MethodDefinition set = new MethodDefinition (
				string.Concat ("set_", prop.Name), (MethodAttributes) 0, prop.PropertyType);
			prop.SetMethod = set;
			return set;
		}

		public PropertyDefinition Clone ()
		{
			return Clone (this, new ImportContext (NullReferenceImporter.Instance, this.DeclaringType));
		}

		internal static PropertyDefinition Clone (PropertyDefinition prop, ImportContext context)
		{
			PropertyDefinition np = new PropertyDefinition (
				prop.Name,
				context.Import (prop.PropertyType),
				prop.Attributes);

			if (prop.HasConstant)
				np.Constant = prop.Constant;

			if (context.GenericContext.Type is TypeDefinition) {
				TypeDefinition type = context.GenericContext.Type as TypeDefinition;
				if (prop.SetMethod != null)
					np.SetMethod = type.Methods.GetMethod (prop.SetMethod.Name, prop.SetMethod.Parameters);
				if (prop.GetMethod != null)
					np.GetMethod = type.Methods.GetMethod (prop.GetMethod.Name, prop.GetMethod.Parameters);
			}

			foreach (CustomAttribute ca in prop.CustomAttributes)
				np.CustomAttributes.Add (CustomAttribute.Clone (ca, context));

			return np;
		}*/
	}
}
